package cn.com.java.service.impl;

import cn.com.java.core.HttpRes;
import cn.com.java.mapper.OrderMapper;
import cn.com.java.mapper.ShoppingCartMapper;
import cn.com.java.model.Order;
import cn.com.java.model.Product;
import cn.com.java.model.ShoppingCart;
import cn.com.java.service.OrderService;
import cn.com.java.service.ProductService;
import cn.com.java.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liushuntao
 * @date 2024/3/19
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final ProductService productService;

    private final ShoppingCartService shoppingCartService;

    @Override
    public String buyOrder(Long productId, Long type, Long userId, Integer num) {
        Order order = new Order();
        Product byId = productService.getById(productId);
        order.setProductId(productId);
        order.setFees(byId.getPrice()*num);
        order.setStatus("1");
        order.setType(type.toString());
        order.setBuyDate(new Date());
        order.setCreateBy(String.valueOf(userId));
        save(order);

        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("product_id", productId);
        ShoppingCart shoppingCart = shoppingCartService.getOne(shoppingCartQueryWrapper);
        if(shoppingCart != null){
            shoppingCart.setStatus("1");
            shoppingCartService.updateById(shoppingCart);
        }
        return "下单成功";
    }

    @Override
    public List<Order> getByUserId(Long userId) {

        return this.baseMapper.selectByUserId(userId);
    }
}
